<template>
  <PageWrapper title="系统密钥对">
    <div class="py-8 bg-white flex flex-col justify-center">
      <Description
        :column="1"
        :data="descriptionData"
        :schema="schema"
        :labelStyle="{ width: '120px' }"
      />
      <div class="flex justify-center" style="margin-top:30px">
        <a-button class="!ml-4" type="primary" color="warning" @click="confirmResetKey"> 重新生成密钥对 </a-button>
      </div>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import { PageWrapper } from '@/components/Page';

  import { Description, DescItem } from '@/components/Description';
  import { queryEncrypt, resetEncrypt } from '@/api/encrypt';
  import { formatToDateTime } from '@/utils/dateUtil';
  import { useMessage } from '@/hooks/web/useMessage';

  const schema: DescItem[] = [
    {
      field: 'public_key',
      label: '公钥',
    },
    {
      field: 'updated_at',
      label: '更新时间',
      render: (curVal) => {
        return formatToDateTime(curVal);
      },
    },
  ];

  const descriptionData = ref({});

  onMounted(async () => {
    descriptionData.value = await queryEncrypt();
  });

  function confirmResetKey() {
    const { createConfirm } = useMessage();
    createConfirm({
        iconType: 'warning',
        title: '警告',
        content: '重新生成密钥后，所有签发的授权书都会失效，请谨慎操作',
        onOk: async () => {
          await handleResetKey();
        },
      });
  }

  async function handleResetKey() {
    descriptionData.value = await resetEncrypt();
  }

</script>